<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.plateNumber" placeholder="车牌号" class="filter-item" clearable style="width: 200px;" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      <el-button v-if="type == 0" v-waves :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>
      <el-button v-if="type == 0" v-waves class="filter-item" type="primary" icon="el-icon-plus" @click="handleCreate">新增黑名单</el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column label="车牌号" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.plateNumber }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属公司" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.company }}</span>
        </template>
      </el-table-column>
      <el-table-column label="黑名单原因" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.reason }}</span>
        </template>
      </el-table-column>
      <el-table-column label="过期时间" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.expireTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.createUser }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.createTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.updateUser }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.updateTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="type == 0" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{ row }">
          <el-button size="mini" icon="el-icon-edit" type="primary" @click="handleEdit(row)">编辑</el-button>
          <el-button size="mini" icon="el-icon-delete" type="danger" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList" />
    <div v-if="dialogAddFormVisible">
      <el-dialog title="新增黑名单" :visible.sync="dialogAddFormVisible" :append-to-body="true" :close-on-click-modal="false" :destroy-on-close="true">
        <BlacklistAddForm ref="blacklistAddForm" :length="total" @close="blacklistAddFormClose" @cancelClose="cancelClose(arguments)" />
      </el-dialog>
    </div>
    <div v-if="dialogEditFormVisible">
      <el-drawer :visible.sync="dialogEditFormVisible" direction="rtl" size="85%" :append-to-body="true" :destroy-on-close="true" :wrapper-closable="false" :with-header="false">
        <BlacklistAddForm :id="editId" ref="blacklistEditForm" @cancelClose="cancelClose(arguments)" />
      </el-drawer>
    </div>
  </div>
</template>

<script>
import { getBlacklist, deleteBlacklist } from '@/api/blacklist';
import waves from '@/directive/waves';
import Pagination from '@/components/Pagination';
import BlacklistAddForm from '@/views/services/blacklistAdd.vue';
import { parseTime } from '@/utils';

export default {
  name: 'Blacklist',
  components: { Pagination, BlacklistAddForm },
  directives: { waves },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      downloadLoading: false,
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        plateNumber: ''
      },
      dialogAddFormVisible: false,
      dialogEditFormVisible: false,
      editId: 0,
      type: null
    };
  },
  created() {
    const { params } = this.$route
    Object.keys(params).forEach(key => {
      this.type = params[key].split(':')[1];
    });
    this.getList();
  },
  methods: {
    async getList() {
      this.listLoading = true;
      await getBlacklist(this.listQuery).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.listLoading = false;
      });
    },
    async handleSearch() {
      this.getList();
    },
    handleCreate() {
      this.dialogAddFormVisible = true;
    },
    async handleDelete(row) {
      await this.$confirm('确定要此条数据吗？', '刪除操作', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          this.listLoading = true;
          await deleteBlacklist(row.id).then(res => {
            this.listLoading = false;
            if (res.msg !== 'OK') {
              this.$message.error(res.msg);
              return;
            }
            this.getList();
            this.$message.success('删除成功');
          });
        })
        .catch(() => {});
    },
    handleEdit(row) {
      this.editId = row.id;
      this.dialogEditFormVisible = true;
    },
    blacklistAddFormClose() {
      this.dialogAddFormVisible = false;
    },
    cancelClose(data) {
      if (data[1] === 'add') {
        this.dialogAddFormVisible = false;
      } else {
        this.dialogEditFormVisible = false;
      }
      if (data[0]) {
        if (data[1] === 'add') {
          this.listQuery.pageNum = 1;
          this.listQuery.pageSize = 10;
          this.listQuery.plateNumber = '';
        }
        this.getList();
      }
    },
    async handleDownload() {
      if (this.total === 0) {
        this.$message.error('暂无数据');
        return;
      }
      this.downloadLoading = true;
      this.listQuery.pageSize = this.total;
      await getBlacklist(this.listQuery).then(response => {
        this.listQuery.pageSize = 10;
        if (response.data.total === 0) {
          this.$message.error('暂无数据，请重新刷新页面');
          return;
        }
        import('@/vendor/Export2Excel').then(excel => {
          const tHeader = ['车牌号', '所属公司', '黑名单原因', '过期时间', '创建人', '创建时间', '更新人', '更新时间'];
          const filterVal = ['plateNumber', 'company', 'reason', 'expireTime', 'createUser', 'createTime', 'updateUser', 'updateTime'];
          const data = this.formatJson(response.data.list, filterVal);
          excel.export_json_to_excel({
            header: tHeader,
            data,
            filename: '黑名单'
          });
          this.downloadLoading = false;
          this.$message({
            message: '黑名单导出成功',
            type: 'success'
          });
        });
      });
    },
    formatJson(list, filterVal) {
      return list.map(v =>
        filterVal.map(j => {
          if (j === 'createTime' || j === 'updateTime' || j === 'expireTime') {
            return parseTime(v[j], '{y}-{m}-{d} {h}:{i}:{s}');
          } else {
            return v[j];
          }
        })
      );
    }
  }
};
</script>
